
#include "StdAfx.h"
#include "MonitorClient.h"
#include "MultiDisplayDlg.h"
#include "LiveViewLayout.h"
//#include "DeviceManager.h"
#include "DBManager.h"

CLiveViewLayout::CLiveViewLayout(int nIndex, CMultiDisplayDlg* pMultiDisplayDlg, const LIVEVIEW_LAYOUT_INFO& LayoutInfo)
: m_LayoutProperty(LayoutInfo.LayoutProperty)
, m_WndChannelIDMap(LayoutInfo.WndChannelIDMap)
, m_pMultiDisplayDlg(pMultiDisplayDlg)
, m_nIndex(nIndex)
{
	//LoadWndChannelMap(LayoutInfo.WndChannelIDMap);
}

CLiveViewLayout::~CLiveViewLayout(void)
{
}


//void CLiveViewLayout::LoadWndChannelMap(const CWndChannelIDMap& WndChannelIDMap)
//{
//	CWndChannelIDMap::const_iterator it = WndChannelIDMap.begin();
//	for(; it != WndChannelIDMap.end(); ++it)
//	{
//		CChannel* pChannel = theDeviceManager.GetChannelByID(it->second);
//		if(pChannel)
//		{
//			m_WndChannelMap[it->first] = pChannel;
//		}
//	}
//}

void CLiveViewLayout::TakeSnapshot()
{
	m_LayoutProperty.nDisplayNum = m_pMultiDisplayDlg->GetDisplayNum();

	m_WndChannelIDMap.clear();

	int nMaxWnd = m_pMultiDisplayDlg->GetMaxDisplayNum();
	for(int i = 0; i < nMaxWnd; i++)
	{
		CMediaPlayer* pMediaPlayer = m_pMultiDisplayDlg->GetMediaPlayer(i);

		if(pMediaPlayer/* && pMediaPlayer->IsPlaying()*/)
		{
			CChannel* pChannel = pMediaPlayer->GetChannel();
			if(pChannel)
				m_WndChannelIDMap[i] = pChannel->GetSipChannelID();
		}
	}

}

BOOL CLiveViewLayout::SetLayoutName(LPCTSTR lpszLayoutName)
{
	//if(!theDBManager->UpdateLiveViewLayoutName(m_LayoutProperty.dwLayoutID, lpszLayoutName))
	//{
	//	return FALSE;
	//}

	m_LayoutProperty.strLayoutName = lpszLayoutName;
	return TRUE;
}

BOOL CLiveViewLayout::SaveLayoutInfo()
{
	if(!theDBManager->UpdateLiveViewLayoutProperty(m_LayoutProperty.dwLayoutID, m_LayoutProperty))
	{
		return FALSE;
	}

	return theDBManager->UpdateLayoutChannelMap(m_LayoutProperty.dwLayoutID, m_WndChannelIDMap);
}

